﻿using System;
using System.Collections.Generic;

namespace RabbitDownload.WebSite.Yande.Tag
{
    internal class YandeTagScanner
    {
        public static void Start(string Tag)
        {
            Tag = Tag.Trim().Replace(" ", "_");
            String API = string.Format(YandeStaticValue.TagApi, "{0}", Tag);

            List<string> ImgList = GetALLImg(API, 0);
            WebCurrency.SaveHelper.AutoSave(ImgList, "Yande", Tag);
        }

        public static void StartForCount(string Tag, int ImgCount)
        {
            Tag = Tag.Trim().Replace(" ", "_");
            String API = string.Format(YandeStaticValue.TagApi, "{0}", Tag);

            List<string> ImgList = GetALLImg(API, ImgCount);
            WebCurrency.SaveHelper.AutoSave(ImgList, "Yande", Tag);
        }

        private static List<string> GetALLImg(string API, int ImgCount)
        {
            int page = 1;
            int JumpFlag = 0;
            List<string> AllImg = new List<string>();
            while (true)
            {
                Console.WriteLine("第{0}页正在扫描", page);

                String TempAPI = string.Format(API, page++);
                Console.WriteLine(TempAPI);
                string Json = YandeHelper.GetJson(TempAPI);

                if (Json != "")
                {
                    List<string> TempList = AnalysisJson(Json);
                    if (TempList == null)
                    {
                        Console.WriteLine("数据格式错误，第{0}次重试", JumpFlag++);

                        if (JumpFlag > 3)
                        {
                            Console.WriteLine("获取图片JSON异常，结束扫描");
                            return AllImg;
                        }
                        continue;
                    }
                    AllImg.AddRange(TempList);

                    if (ImgCount != 0 && (ImgCount - AllImg.Count) < 0)
                    {
                        Console.WriteLine("达到预设数量，跳出");
                        break;
                    }
                }
            }

            Console.WriteLine("扫描结束,总数" + AllImg.Count);
            return AllImg;
        }

        /// <summary>
        /// 分析json返回结果
        /// </summary>
        /// <param name="json"></param>
        /// <returns></returns>
        private static List<string> AnalysisJson(string json)
        {
            List<YandeJsonClass.PostItem> list = YandeHelper.InitPostJson(json);
            List<string> ImgList = new List<string>();

            if (list == null)
            {
                return null;
            }
            foreach (YandeJsonClass.PostItem Single in list)
            {
                Console.WriteLine(Single.id);
                if (YandeStaticValue.ExcludeFlag && YandeHelper.GetJudgeTags(Single.tags))
                {
                    Console.WriteLine("检测到黑名单关键词，跳出！");
                }

                if (!YandeHelper.GetModeFlag(Single.rating, Single.score))
                {
                    continue;
                }

                ImgList.Add(Single.file_url);
            }
            return ImgList;
        }
    }
}